var system = require('system');
var root_url = system.args[1];
// console.log(system.args.length);
var fs = require('fs');
var output = system.args[2];
var page = require('webpage').create();
var system = require('system');
var webPage = require('webpage');
var fs = require('fs');
var page = webPage.create();
var countNum = 0;
page.onLoadFinished = function(status) {
console.log(countNum);
page.evaluate(function() {
			var mouseOverEv = document.createEvent("MouseEvents");
			mouseOverEv.initEvent("mouseover",false,true);
			var elementSearchOption = document.querySelector('a[name="tj_settingicon"]');
			console.log(elementSearchOption.value);
			elementSearchOption.dispatchEvent(mouseOverEv);
			var mouseClickEv = document.createEvent("MouseEvents");
			var elementAdSearchOption = document.querySelectorAll('.bdpfmenu')[1];
			var aElement = elementAdSearchOption.querySelectorAll('a')[1];
			mouseClickEv.initEvent("click",false, true);
			aElement.dispatchEvent(mouseClickEv);
	});		
	page.render("baidu.jpg");
	// console.log(document.querySelector('body').innerHtml;
	fs.write(output,page.content,'w');
};

function openPage(root_url) {
	page.open(root_url, function(status) {
		if (status === "success") {
			console.log('page open is success');
			phantom.exit();
		} else {
			console.log('page open is not success,phantomjs will exit!');
			exitPhantom();
		}
		// console.log('page.open is finished');
	});
}

function closePage() {
	page.close();
}

function exitPhantom() {
	phantom.exit();
}

openPage(root_url);

